package studysystem.demo.tool;

import org.apache.catalina.Manager;
import org.springframework.beans.factory.annotation.Autowired;
import studysystem.demo.bean.Permission;
import studysystem.demo.bean.Learner;
import studysystem.demo.bean.Sys_Manager;
import studysystem.demo.mapper.LearnerMapper;
import studysystem.demo.mapper.Sys_ManagerMapper;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import studysystem.demo.mapper.ManagerMapper;

import java.util.ArrayList;
import java.util.List;
//学习者的登录认证
public class AuthRealm extends AuthorizingRealm {

    @Autowired
    private LearnerMapper learnerMapper;

    @Autowired
    private ManagerMapper managerMapper;

    @Autowired
    private Sys_ManagerMapper sys_managerMapper;
    //认证.登录
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.err.println("--------doGetAuthenticationInfo--------");
        UsernamePasswordToken utoken=(UsernamePasswordToken) token;//获取用户输入的token
        String phone = utoken.getUsername();
        System.out.println("utoken phone"+phone);
        if(phone.length()==11) {
            Learner user = learnerMapper.findLearnerInfo(phone);
            //System.out.println(user.getPhone()+user.getPassword());
            return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName());//放入shiro.调用CredentialsMatcher检验密码
        }else if (phone.length()==10){
            studysystem.demo.bean.Manager user = managerMapper.findAdminInfo(phone);
            return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName());//放入shiro.调用CredentialsMatcher检验密码
        }else {
            Sys_Manager user =sys_managerMapper.findSys_ManagerInfo(phone);
            return new SimpleAuthenticationInfo(user, user.getPassword(), this.getClass().getName());//放入shiro.调用CredentialsMatcher检验密码
        }

    }

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        System.err.println("--------doGetAuthorizationInfo--------");
        Learner user=(Learner) principal.fromRealm(this.getClass().getName()).iterator().next();//获取session中的用户
        System.out.println(user.getName());
        List<String> per=new ArrayList<>();
        String role = learnerMapper.findRole(user.getName()).getRole();
        System.out.println(role);
        List<Permission> permissions = learnerMapper.findPermission(role);
        if(permissions.size()>0) {
            for(Permission permission : permissions) {
                per.add(permission.getPermission());
                System.out.println(permission.getPermission());
            }
        }
        System.out.print(permissions.size());
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        info.addStringPermissions(per);//将权限放入shiro中.
        return info;
    }

}

